'''
Created on 2010-4-8

@author: chengmeng
'''
import XmlScene
import environment
import wx

wildcard = 	"scene file (*.scene)|*.scene|"     \
   			"All files (*.*)|*.*"


class Plugin:

	def __init__(self,frame):
		ID_LoadTest = wx.NewId()
		ID_OpenScene = wx.NewId()
		ID_SaveScene = wx.NewId()
		ID_SaveAs = wx.NewId()
		ID_ClearScene = wx.NewId()

		pos = 0
		if environment.isDebug :
			frame.GetMenuBar().GetMenu(0).Insert(pos, ID_LoadTest, "load test")
			pos += 1
		frame.GetMenuBar().GetMenu(0).Insert(pos, ID_OpenScene, "&Open\tCtrl-O")
		pos += 1
		frame.GetMenuBar().GetMenu(0).Insert(pos, ID_SaveScene, "&Save\tCtrl-S")
		pos += 1
		frame.GetMenuBar().GetMenu(0).Insert(pos, ID_SaveAs, "Save &As")
		pos += 1
		frame.GetMenuBar().GetMenu(0).Insert(pos, ID_ClearScene, "&Clear Scene")
		pos += 1
		frame.GetMenuBar().GetMenu(0).InsertSeparator(pos)

		frame.Bind(wx.EVT_MENU, self.LoadTest, 		id=ID_LoadTest)
		frame.Bind(wx.EVT_MENU, self.OpenScene, 	id=ID_OpenScene)
		frame.Bind(wx.EVT_MENU, self.SaveScene, 	id=ID_SaveScene)
		frame.Bind(wx.EVT_MENU, self.SaveAs, 		id=ID_SaveAs)
		frame.Bind(wx.EVT_MENU, self.ClearScene, 	id=ID_ClearScene)

		self.frame = frame
		pass

	def LoadTest(self,event):
		#load('F:\\magiccarpet\\trunk\\bin\\Media\\scenes\\scene11_zjc\\scene11_zjc.scene')
		sf = "F:\\magiccarpet\\trunk\\bin\\Media\\scenes\\scene00\\scene00.scene"
		
		self.ClearScene()
		
		self.lastPath = sf
		
		self.frame.plugins.beginLoadScene()
		
		XmlScene.load(sf,self.frame.plugins.OgreWindowPlugin.getSceneManager())
		
		self.frame.plugins.endLoadScene()

		pass


	def OpenScene(self,event):
		dlg = wx.FileDialog(self.frame, "Choose File", wildcard = wildcard, style = wx.FD_OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			self.ClearScene()
			
			self.lastPath = dlg.GetPath()

			self.frame.plugins.beginLoadScene()
			
			XmlScene.load(dlg.GetPath(),self.frame.plugins.OgreWindowPlugin.getSceneManager(),self.frame.plugins)
			
			self.frame.plugins.endLoadScene()

		dlg.Destroy()
		self.frame.SetFocus()
		pass

	def SaveScene(self,event):
		if hasattr(self,'lastPath'):
			XmlScene.save(self.lastPath,self.frame.plugins.OgreWindowPlugin.getSceneManager(),self.frame.plugins)
		else :
			self.SaveAs()
		pass

	def SaveAs(self,event = None):
		dlg = wx.FileDialog(
		 	self.frame, message="Save file as ...",
		    defaultFile="", wildcard=wildcard, style=wx.SAVE
		    )

		# This sets the default filter that the user will initially see. Otherwise,
		# the first filter in the list will be used by default.
		#dlg.SetFilterIndex(2)

		if dlg.ShowModal() == wx.ID_OK:
			self.lastPath = dlg.GetPath()
			XmlScene.save(self.lastPath,self.frame.plugins.OgreWindowPlugin.getSceneManager(),self.frame.plugins)

		pass

	def ClearScene(self,event = None):

		if hasattr(self,'lastPath') and len(self.lastPath) != 0:
			import ogre.renderer.OGRE as ogre 
			ogre.ResourceGroupManager.getSingleton().destroyResourceGroup(self.lastPath)
			del self.lastPath

		self.frame.plugins.clearScene()
		pass


##
def IInit(frame):
	return Plugin(frame)

